﻿function buildCkFinder(state, selectAction, selectTitle,showSelectPic) {
    if (state == 1) {
        ckFinder_selectAction = selectAction;
        if (showSelectPic) ckFinder_showSelectPic = showSelectPic;
        else ckFinder_showSelectPic = true;
        if (selectTitle && selectTitle != "") $("#ckFinder_title").html(selectTitle);
        else $("#ckFinder_title").html("选择图片");

        if ($("#ckFinder_con").html() == "") {
            $("#ckFinderBox").hide(); finder.appendTo($("#ckFinder_con")[0]);
        }
        else if ($("#ckFinderBox").is(":hidden")) CkFinder_load();

    } else {
        closeCkFinder();
    }
}
function CkFinder_load() {
    var ckfinder_iframe = $("#ckFinderBox iframe");
    var iframebody = ckfinder_iframe[0].contentWindow.document.body;
    if (iframebody) {
        $(iframebody).find("#folders_view").css({ height: "100%" }).find(".tool_panel").remove();
    }

    $("#ckFinderBox").find("iframe").hide().end().show().css({ width: 0, left: admin_right.offset().left, opacity: 1 })
    .animate({ width: admin_right.offset().left,left: 0 },"fast", function () {
        
        $("#ckFinderBox").find("iframe").height($("#ckFinderBox").height() - $("#ckFinder_title").outerHeight(true)).fadeIn();
    })
}
function closeCkFinder() {
    //$("#ckFinderBox").animate({ width: 0 });
    $("#ckFinderBox").find("iframe").hide();
    $("#ckFinderBox").animate({ left: admin_right.offset().left },"fast", function () {
        $("#ckFinderBox").hide();
    });
    //$("#ckFinderBox").find("iframe").fadeOut(function () {
    //    $("#ckFinderBox").animate({ left: admin_right.offset().left }, function () {
    //        $("#ckFinderBox").hide();
    //    });
    //});
}

function selectActionFunction(fileUrl, data, allFiles) {
    var msg = 'The last selected file is: <a href="' + fileUrl + '">' + fileUrl + '</a><br /><br />';
    // Display additional information available in the "data" object.
    // For example, the size of a file (in KB) is available in the data["fileSize"] variable.
    if (fileUrl != data['fileUrl'])
        msg += '<b>File url:</b> ' + data['fileUrl'] + '<br />';
    msg += '<b>File size:</b> ' + data['fileSize'] + 'KB<br />';
    msg += '<b>Last modified:</b> ' + data['fileDate'];

    if (allFiles.length > 1) {
        msg += '<br /><br /><b>Selected files:</b><br /><br />';
        msg += '<ul style="padding-left:20px">';
        for (var i = 0 ; i < allFiles.length ; i++) {
            // See also allFiles[i].url
            msg += '<li>' + allFiles[i].data['fileUrl'] + ' (' + allFiles[i].data['fileSize'] + 'KB)</li>';
            // var element = new CKEDITOR.dom.element("<img src=\""+allFiles[i].data['fileUrl']+"\"/>");
            //element.setText(text);
            // admin_right.CKEDITOR.insertElement(element);
            //top.admin_right.insertImgCkeditor(allFiles[i].data['fileUrl']);
        }
        msg += '</ul>';
    }
    // this = CKFinderAPI object
    //this.openMsgDialog("Selected file", msg);




    switch (ckFinder_selectAction) {
        case "single":
            if (ckfinderImgGetter_Dom && ckfinderImgGetter_Input) {
                if (ckFinder_showSelectPic) {
                    ckfinderImgGetter_Dom.hide().attr("src", fileUrl).show("fast", function () {
                        ckfinderImgGetter_Dom.siblings(".picdeler").show();
                    });
                }
                ckfinderImgGetter_Input.val(fileUrl);
                
            }
            break;
        case "multi":
            for (var i = 0 ; i < allFiles.length ; i++) {
                frame_right[0].contentWindow.BuildMultiPic(allFiles[i].data['fileUrl']);
            }
            break;
        default:
            if (frame_right[0].contentWindow.FormCkeditor) {
                for (var i = 0 ; i < allFiles.length ; i++) {
                    frame_right[0].contentWindow.FormCkeditor.insertHtml("<img src='" + allFiles[i].data['fileUrl'] + "'/>")
                    //  frame_right[0].contentWindow.insertImgCkeditor(allFiles[i].data['fileUrl']);
                }
            }
            break;
    }

}
function selectSingle(fileUrl, data, allFiles) {
    if (ckfinderImgGetter_Dom && ckfinderImgGetter_Input) {
        ckfinderImgGetter_Dom.hide().attr("src", fileUrl).show("fast");
        ckfinderImgGetter_Input.val(fileUrl);
    }
}

function full_adminRight(isFull) {
    if (isFull) {
        frame_right.attr("data-width", frame_right.width()).css({ width: "100%" });
        admin_right.attr("data-width", admin_right.width()).attr("data-left",admin_right.offset().left).css({ width: "100%",left:0 }, "fast");
        $("#close_right").hide();
    }
    else {
        frame_right.width(frame_right.attr("data-width"));
        admin_right.css({ width: admin_right.attr("data-width")*1, left: admin_right.attr("data-left")*1 });
        $("#close_right").show();
    }
}